<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>奖品管理</title>
<meta name="viewport" content="width=100%, initial-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="/lib/weix/style/weui.min.css">
<link rel="stylesheet" href="/css/fonts.css">
<link rel="stylesheet" href="/css/comm.css">
<link rel="stylesheet" href="/css/list_jianp.css">
</head>

<body>
<div class="th-heads__boxs">
	<div class="th-heads__box th-xx_bot" id="J_tab-title">
        <a class="th-heads__itme on" href="javascript:;" data-fw="兑换中">兑换中</a>
        <a class="th-heads__itme" href="javascript:;" data-fw="库存少">库存少</a>
        <a class="th-heads__itme" href="javascript:;" data-fw="已下架">已下架</a>
    </div>
</div>

<div class="jiap-boxs th-xx_bot" style="margin-top:.625em" id="J_list">

</div>


<div class="weui-loadmore" id="J_loadmore">
    <i class="weui-loading"></i>
    <span class="weui-loadmore__tips">正在加载</span>
</div>

<div class="jiap-bt__ansbox"><a class="jiap-bt__ans" href="/productPage/productEdit"><i class="icon-plus"></i>添加礼品</a></div>


<div class="th-tcc__boxs" id="J_boxs_czs">
    <div class="th-tcc__bj" style="background:none;"></div>
    <div class="th-tcc__box th-tcc__box_czs" id="J_box_czs" style="top:20px; right:30px;">
        <span class="jo"><i class="icon-arrow"></i></span>
        <div class="th-tcc__hsbox_czs">
            <div class="th-tcc__hs_czs">
                <a class="th-tcc__ans_czs th-xx_left" href="javascript:;" id="J_czs_del"><i class="icon-trash"></i>删除</a>
                <a class="th-tcc__ans_czs th-xx_left" href="javascript:;" id="J_czs_sx">
                    <span id="J_czs_sx_1" style="display:none;"><i class="icon-download"></i>下架</span>
                    <span id="J_czs_sx_2" style="display:none;"><i class="icon-upload"></i>上架</span>
                </a>
                <a class="th-tcc__ans_czs th-xx_left" href="javascript:;" id="J_czs_bj"><i class="icon-edit"></i>编辑</a>
            </div>
        </div>
    </div>
</div>

<script src="/js/zepto.min.js"></script>
<script src="https://res.wx.qq.com/open/libs/weuijs/1.0.0/weui.min.js"></script>
<script src="/js/comm.js"></script>
<script type="text/javascript">
//列表
function getProductList(page,type){
	var uid = "${uid}";
	var data = {page:page,rows:100,uid:uid,type:type};   //type 1:兑换中，2：库存少，3：已下架
	var url = "/product/product_list";
	
	$('#J_loadmore').show()
	
	$.ajax({
		type: "POST",
		dataType: 'json',
		async: false,
		contentType: "application/json; charset=utf-8",
		data: JSON.stringify(data),
		url: url,
		success: function(res){
			if(res.code == 200){
				//操作
				var list = res.data.list;
				//console.log(list);
				for (var i =0; i<list.length;i++){
					
					var shtml = '<div class="jiap-box th-xx_top J_data">'+
									'<a class="jiap-box__pic" href="javascript:;"><img src="'+list[i]['pic']+'" /></a>'+
									'<div class="jiap-box__info">'+
										'<a class="jiap-box__hd"  href="javascript:;">'+list[i]['title']+'</a>'+
										'<div class="jiap-box__cz_box">'+
											'<div class="jiap-box__cz_itme">'+
												'<p><span class="s">'+list[i]['point']+'</span>积分</p>'+
												'<p class="hs">￥'+list[i]['price']+'</p>'+
											'</div>'+
											'<div class="jiap-box__cz_itme th-xx_left">'+
												'<p>库存'+list[i]['stock']
							if(list[i]['stock'] < 1){
							shtml +=				'<span class="k">缺货</span></p>'	
							}else if(1 < list[i]['stock'] < 10){
							shtml +=				'<span class="k k_1">告急</span></p>'	
							}
							shtml +=			'</p>'+
												'<p class="hs">已兑换'+list[i]['exchange']+'</p>'+
											'</div>'+
											'<div class="jiap-box__cz_itme">'+
												'<a class="jiap-box__an J_caz" href="javascript:;" data-id="'+list[i]['id']+'" data-ups="'+list[i]['isup']+'"><span class="qs"></span></a>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'
								
					$('#J_list').append(shtml);
				}
			}else{
				weui.topTips(res.msg);
			}
		},
		error: function(e){

		},
		complete: function(e){
			$('#J_loadmore').hide()
		}
	});
}

$(function(){
	//-----------------------------------------------
	//初始化
	getProductList(1,1);
	//-----------------------------------------------
	//弹出操作菜单
	var $this_data = [];
	$(document).on('click','.J_caz',function(){
		var $this_t = $(this);
		var scrollTop = document.body.scrollTop+document.documentElement.scrollTop;
		var x = $this_t.offset().top-scrollTop-10
		var y = document.body.clientWidth - $this_t.offset().left+5
		var id = $this_t.attr('data-id')
		var up_data = $this_t.attr('data-ups')
		
		$this_data = $(this)
		
		if(up_data == 0){
			$('#J_czs_sx_1').show()
			$('#J_czs_sx_2').hide()
		}else if(up_data == 1){
			$('#J_czs_sx_1').hide()
			$('#J_czs_sx_2').show()
		}
		
		$('#J_boxs_czs').addClass('on')
		$('#J_box_czs').attr('data-id',id).css('top',x).css('right',y).addClass('on')
		
		$('#J_boxs_czs').addClass('on')
		$('#J_box_czs').attr('data-id',id).css('top',x).css('right',y).addClass('on')
	})
	
	//删除
	$(document).on('click','#J_czs_del',function(){
		var id = $('#J_box_czs').attr('data-id')
		
		weui.dialog({
			title: '确定删除',
			content: '是否确定删除该奖品？',
			className: 'custom-classname',
			buttons: [{
				label: '取消',
				type: 'default',
				onClick: function () {}
			}, {
				label: '确定',
				type: 'primary',
				onClick: function () {
					 
					var loading = weui.loading('数据提交中', {
						className: 'custom-classname'
					});
					
					var data = {id:id};
					var url = "/product/product_delete";
					$.ajax({
						type: "POST",
						dataType: 'json',
						async: false,
						contentType: "application/json; charset=utf-8",
						data: JSON.stringify(data),
						url: url,
						success: function(res){
							if(res.code == 200){
								loading.hide();
								weui.toast('删除成功', {
									duration: 500,
									className: 'custom-classname',
									callback: function(){ 
										$this_data.parents('.J_data').remove();
									}
								});	
							}else{
								weui.topTips(res.msg);
							}
						},
						error: function(e){
				
						},
						complete: function(e){
							
						}
					});					 
				}
			}]
		});
		
	})
	
	//上下架
	$(document).on('click','#J_czs_sx',function(){
		var id = $('#J_box_czs').attr('data-id')
		var up = $this_data.attr('data-ups')
		
		var loading = weui.loading('数据提交中', {
			className: 'custom-classname'
		});
		
		var up_data = 100;
		var tis_data = ''
		if(up == 0){
			up_data = 1
			tis_data = '下架'
		}else{
			up_data = 0
			tis_data = '上架'
		}
		
		var data = {id:id,up:up_data};
		var url = "/product/product_up";
		$.ajax({
			type: "POST",
			dataType: 'json',
			async: false,
			contentType: "application/json; charset=utf-8",
			data: JSON.stringify(data),
			url: url,
			success: function(res){
				if(res.code == 10003){
					loading.hide();
					weui.toast(tis_data+'成功', {
						duration: 500,
						className: 'custom-classname',
						callback: function(){ 
							$this_data.parents('.J_data').remove();
						}
					});
					
				}else{
					weui.topTips(res.msg);
				}
			},
			error: function(e){
	
			},
			complete: function(e){
				
			}
		});
	})
	
	//编辑
	$(document).on('click','#J_czs_bj',function(){
		var id = $('#J_box_czs').attr('data-id')

		location.href = '/productPage/productEdit?id='+id+'';
	})
	//-----------
	//关闭操作菜单
	$(document).on('touchend','#J_boxs_czs',function(){
		$('#J_boxs_czs').removeClass('on')
		$('#J_box_czs').removeClass('on')
	})
	$(document).on('touchmove','#J_boxs_czs',function(){
		$('#J_boxs_czs').removeClass('on')
		$('#J_box_czs').removeClass('on')
	})
	$(window).resize(function() {
		$('#J_boxs_czs').removeClass('on')
		$('#J_box_czs').removeClass('on')
	});
	//-----------------------------------------------
	//切换分类
	$(document).on('click', '#J_tab-title a', function(){
		var fw = $(this).attr('data-fw')
		$(this).addClass('on').siblings().removeClass('on'); 
		
		$('#J_list').html('');
		
		if(fw == '兑换中'){
			getProductList(1,1);
		}
		
		if(fw == '库存少'){
			getProductList(1,2);
		}
		
		if(fw == '已下架'){
			getProductList(1,3);
		}

	})

});
</script>
</body>
</html>